home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / ASM-PPC / CHECKSUM.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  3KB  |  114 lines

  1. #ifndef _PPC_CHECKSUM_H
  2. #define _PPC_CHECKSUM_H
  3.  
  4.  
  5. /*
  6.  * computes the checksum of a memory block at buff, length len,
  7.  * and adds in "sum" (32-bit)
  8.  *
  9.  * returns a 32-bit number suitable for feeding into itself
  10.  * or csum_tcpudp_magic
  11.  *
  12.  * this function must be called with even lengths, except
  13.  * for the last fragment, which may be odd
  14.  *
  15.  * it's best to have buff aligned on a 32-bit boundary
  16.  */
  17. extern unsigned int csum_partial(const unsigned char * buff, int len,
  18.                  unsigned int sum);
  19.  
  20. /*
  21.  * Computes the checksum of a memory block at src, length len,
  22.  * and adds in "sum" (32-bit), while copying the block to dst.
  23.  * If an access exception occurs on src or dst, it stores -EFAULT
  24.  * to *src_err or *dst_err respectively (if that pointer is not
  25.  * NULL), and, for an error on src, zeroes the rest of dst.
  26.  *
  27.  * Like csum_partial, this must be called with even lengths,
  28.  * except for the last fragment.
  29.  */
  30. extern unsigned int csum_partial_copy_generic(const char *src, char *dst,
  31.                           int len, unsigned int sum,
  32.                           int *src_err, int *dst_err);
  33.  
  34. #define csum_partial_copy_from_user(src, dst, len, sum, errp)    \
  35.     csum_partial_copy_generic((src), (dst), (len), (sum), (errp), 0)
  36.  
  37. /* FIXME: this needs to be written to really do no check -- Cort */
  38. #define csum_partial_copy_nocheck(src, dst, len, sum)    \
  39.     csum_partial_copy_generic((src), (dst), (len), (sum), 0, 0)     
  40. /*
  41.  * Old versions which ignore errors.
  42.  */
  43. #define csum_partial_copy(src, dst, len, sum)    \
  44.     csum_partial_copy_generic((src), (dst), (len), (sum), 0, 0)
  45. #define csum_partial_copy_fromuser(src, dst, len, sum)    \
  46.     csum_partial_copy_generic((src), (dst), (len), (sum), 0, 0)
  47.  
  48.  
  49. /*
  50.  * turns a 32-bit partial checksum (e.g. from csum_partial) into a
  51.  * 1's complement 16-bit checksum.
  52.  */
  53. static inline unsigned int csum_fold(unsigned int sum)
  54. {
  55.     unsigned int tmp;
  56.  
  57.     /* swap the two 16-bit halves of sum */
  58.     __asm__("rlwinm %0,%1,16,0,31" : "=r" (tmp) : "r" (sum));
  59.     /* if there is a carry from adding the two 16-bit halves,
  60.        it will carry from the lower half into the upper half,
  61.        giving us the correct sum in the upper half. */
  62.     sum = ~(sum + tmp) >> 16;
  63.     return sum;
  64. }
  65.  
  66. /*
  67.  * this routine is used for miscellaneous IP-like checksums, mainly
  68.  * in icmp.c
  69.  */
  70. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  71. {
  72.     return csum_fold(csum_partial(buff, len, 0));
  73. }
  74.  
  75. /*
  76.  * FIXME: I swiped this one from the sparc and made minor modifications.
  77.  * It may not be correct.  -- Cort
  78.  */
  79. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  80.                            unsigned long daddr,
  81.                            unsigned short len,
  82.                            unsigned short proto,
  83.                            unsigned int sum) 
  84. {
  85.     __asm__("
  86.     addc %0,%0,%1
  87.     adde %0,%0,%2
  88.     adde %0,%0,%3
  89.     addze %0,%0
  90.     "
  91.     : "=r" (sum)
  92.     : "r" (daddr), "r"(saddr), "r"((proto<<16)+len), "0"(sum));
  93.     return sum;
  94. }
  95.  
  96. /*
  97.  * This is a version of ip_compute_csum() optimized for IP headers,
  98.  * which always checksum on 4 octet boundaries.  ihl is the number
  99.  * of 32-bit words and is always >= 5.
  100.  */
  101. extern unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl);
  102.  
  103. /*
  104.  * computes the checksum of the TCP/UDP pseudo-header
  105.  * returns a 16-bit checksum, already complemented
  106.  */
  107. extern unsigned short csum_tcpudp_magic(unsigned long saddr,
  108.                     unsigned long daddr,
  109.                     unsigned short len,
  110.                     unsigned short proto,
  111.                     unsigned int sum);
  112.  
  113. #endif
  114.